from rest_framework import serializers
from orders.models import OrderInfo, OrderGoods
from goods.models import SKU

class SKUSerializer(serializers.ModelSerializer):
    """
    sku
    """
    class Meta:
        # 指定根据哪个模型类生成序列化器字段
        model = SKU
        # 指定哪些字段生成
        fields = ('name', 'default_image')

class OrderGoodsSerializer(serializers.ModelSerializer):
    """
    订单商品表数据
    """
    sku = SKUSerializer()
    class Meta:
        # 指定根据哪个模型类生成序列化器字段
        model = OrderGoods
        # 指定哪些字段生成
        fields = ('count', 'price', 'sku')

class  OrderSerializer(serializers.ModelSerializer):
    """
    订单表序列化器
    """
    user = serializers.StringRelatedField(read_only=True)
    address = serializers.PrimaryKeyRelatedField(read_only=True)
    # 订单商品表关联订单表
    skus = OrderGoodsSerializer(many=True)
    class Meta:
        # 指定根据哪个模型类生成序列化器字段
        model = OrderInfo
        # 指定哪些字段生成
        fields = "__all__"
